package cn.tedu.antcolony.market.dao.persist.repository.impl;


import cn.tedu.antcolony.market.dao.persist.mapper.ClueMapper;
import cn.tedu.antcolony.market.dao.persist.repository.IClueRepository;
import cn.tedu.antcolony.market.pojo.entity.Clue;
import cn.tedu.antcolony.market.pojo.vo.ClueListItemVO;
import cn.tedu.antcolony.market.pojo.vo.ClueStandardVO;
import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 处理线索数据的存储库实现类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Repository
//返回值为操作数据库的返回值
public class ClueRepositoryImpl implements IClueRepository {

    @Autowired
    private ClueMapper clueMapper;

    public ClueRepositoryImpl() {
        log.debug("创建数据访问实现类对象：ClueRepositoryImpl");
    }

    @Override
    public int insert(Clue clue) {
        log.debug("开始执行【插入线索】的数据访问，参数：{}", clue);
        return clueMapper.insert(clue);
    }

    @Override
    public int insertBatch(List<Clue> clueList) {
        log.debug("开始执行【批量插入线索】的数据访问，参数：{}", clueList);
        return clueMapper.insertBatch(clueList);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除线索】的数据访问，参数：{}", id);
        return clueMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<String> idList) {
        log.debug("开始执行【批量删除线索】的数据访问，参数：{}", idList);
        return clueMapper.deleteBatchIds(idList);
    }

    @Override
    public int update(Clue clue) {
        log.debug("开始执行【更新线索】的数据访问，参数：{}", clue);
        return clueMapper.updateById(clue);
    }

    @Override
    public int count() {
        log.debug("开始执行【统计线索的数量】的数据访问，参数：无");
        return clueMapper.selectCount(null);
    }

    @Override
    public int countByPhone(String phone) {
        log.debug("开始执行【统计匹配手机号的线索的数量】的数据访问，参数：{}", phone);
        QueryWrapper<Clue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        return clueMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByNameAndNotId(Long id, String name) {
        log.debug("开始执行【统计匹配ID但不匹配名称的线索的数量】的数据访问，ID：{}，名称：{}", id, name);
        QueryWrapper<Clue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name).ne("id", id);
        return clueMapper.selectCount(queryWrapper);
    }

    @Override
    public ClueStandardVO getStandardByClueId(Long id) {
        log.debug("开始执行【根据ID查询线索信息】的数据访问，参数：{}", id);
        return clueMapper.getStandardByClueId(id);
    }

    @Override
    public ClueStandardVO getStandardByCustomerSeaId(Long id) {
        log.debug("开始执行【根据ID查询线索信息】的数据访问，参数：{}", id);
        return clueMapper.getStandardByCustomerSeaId(id);
    }

    @Override
    public PageData<ClueListItemVO> listClue(Integer page, Integer pageSize) {
        log.debug("开始执行【查询线索列表】的数据访问，页码：{}，每页记录数：{}", page, pageSize);
        PageHelper.startPage(page, pageSize);
        List<ClueListItemVO> list = clueMapper.listClue();
        PageInfo<ClueListItemVO> pageInfo = new PageInfo<>(list);
        PageData<ClueListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("返回数据：{}",pageData);
        return pageData;
    }

    @Override
    public PageData<ClueListItemVO> listCustomerSea(Integer page, Integer pageSize) {
        log.debug("开始执行【查询公海列表】的数据访问，页码：{}，每页记录数：{}", page, pageSize);
        PageHelper.startPage(page, pageSize);
        List<ClueListItemVO> list = clueMapper.listCustomerSea();
        PageInfo<ClueListItemVO> pageInfo = new PageInfo<>(list);
        PageData<ClueListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public PageData<ClueListItemVO> list(Integer page, Integer pageSize) {
        log.debug("开始执行【查询线索和公海公海列表】的数据访问，页码：{}，每页记录数：{}", page, pageSize);
        PageHelper.startPage(page, pageSize);
        List<ClueListItemVO> list = clueMapper.list();
        PageInfo<ClueListItemVO> pageInfo = new PageInfo<>(list);
        PageData<ClueListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }

    @Override
    public int transformToSeaById(Long id) {
        log.debug("开始执行【根据ID转入公海】的操作，参数：{}", id);
        return clueMapper.transformToSeaById(id);
    }

    @Override
    public int transformToClueById(Long id) {
        log.debug("开始执行【根据ID领取公海】的操作，参数：{}", id);
        return clueMapper.transformToClueById(id);
    }

    @Override
    public int changeAttribution(Long id,String name){
        log.debug("开始执行【修改归属人】的操作，参数：{},归属人{}", id,name);
        return clueMapper.changeAttribution(id,name);
    }

    @Override
    public int deleteAttribution(Long id){
        log.debug("开始执行【根据ID删除归属人】的操作，参数：{}", id);
        return clueMapper.deleteAttribution(id);
    }

}
